app.controller('TagCtrl', ['$scope', '$timeout', '$http', '$compile', 'R', 'toaster', '$modal', '$rootScope', '$location', function($scope, $timeout, $http, $compile, R, toaster, $modal, $rootScope, $location) {
    $scope.currentpage = 20;
    $scope.totalCount = 0;

    $scope.query = {
        keyword: "",
        pageNo: 0,
        pageSize: $scope.currentpage
    }

    $scope.opened = [];
    $scope.open = function($event,index) {
        $event.preventDefault();
        $event.stopPropagation();
        $scope.opened[index] = true;
    };

    $scope.submit = function(){
        $scope.query.pageNo = 0;
        $scope.getItems();
    }

    $scope.del = function(item){
        $rootScope.confirm('确认删除该标签?', '提示信息')
        .then(function () {
            R.deleteTag({
                id: item.id
            }).then(function(){
                toaster.success('操作成功');
                $scope.getItems();
            });
        });
    }

    $scope.rowCollectionPage = [];

    $scope.edit = function(row){
        $rootScope.setSgObj('TAGONE', row);

        $location.url("/app/tag/one?id=" + row.id);
    }

    $scope.getItems = function(tableState, ctrl) {
        if (!$scope.stCtrl && ctrl) {
            $scope.stCtrl = ctrl;
        }

        if (!tableState && $scope.stCtrl) {
            $scope.stCtrl.pipe();
            return;
        }

        var pagination = tableState.pagination;

        $scope.query.pageNo = Math.ceil(pagination.start / $scope.currentpage);

        R.getTagsByCondition($scope.query).then(function(data) {
            $scope.rowCollectionPage = data.bizData.list;
            $scope.totalCount = data.bizData.count;
            $scope.pages = tableState.pagination.numberOfPages = Math.ceil(data.bizData.count / $scope.currentpage);
        });
    };
}]);